<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Simple Cookie Example</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Simple Cookie Example</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
<p>This example shows basic usage of the YUI Cookie utility. The example checks the value of a cookie and then sets it to a new value.</p>
</div>

<div class="example yui3-skin-sam">
  <script>
YUI().use('cookie', 'console', function (Y) {

    //used just to show logging output
    (new Y.Console({style: 'block'})).render('.example');
    
    var currentValue = Y.Cookie.get("example");
    Y.log("Cookie's current value is '" + currentValue + "'");

    var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000);
    Y.log("Setting cookie's value to '" + newValue + "'");
    Y.Cookie.set("example", newValue);

});
</script>
</div>

<h2>Description</h2>

<p>This example begins by getting the value of a cookie named &quot;example&quot;. If this is the first time you've run
the example, the value should be <code>null</code>:

<pre class="code prettyprint">var currentValue = Y.Cookie.get(&quot;example&quot;);</pre>


<p>This value is shown in the browser console. Next, the cookie is set to a random value:</p>

<pre class="code prettyprint">var newValue = &quot;yui&quot; + Math.round(Math.random() * Math.PI);
Y.Cookie.set(&quot;example&quot;, newValue);</pre>


<p>When you reload the page, the value of the cookie should be the one that was just set.</p>

<p>Note: this example uses session cookies, so the value will be lost when you close the browser.</p>

<h2>Complete Example Source</h2>

<pre class="code prettyprint">&lt;script&gt;
YUI().use(&#x27;cookie&#x27;, &#x27;console&#x27;, function (Y) {

    &#x2F;&#x2F;used just to show logging output
    (new Y.Console({style: &#x27;block&#x27;})).render(&#x27;.example&#x27;);
    
    var currentValue = Y.Cookie.get(&quot;example&quot;);
    Y.log(&quot;Cookie&#x27;s current value is &#x27;&quot; + currentValue + &quot;&#x27;&quot;);

    var newValue = &quot;yui&quot; + Math.round(Math.random() * Math.PI * 1000);
    Y.log(&quot;Setting cookie&#x27;s value to &#x27;&quot; + newValue + &quot;&#x27;&quot;);
    Y.Cookie.set(&quot;example&quot;, newValue);

});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Demonstrates basic usage of the Cookie utility for reading and writing cookies.">
                                        <a href="cookie-simple-example.html">Simple Cookie Example</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Demonstrates using the Cookie utility to get, set and remove cookies.">
                                        <a href="cookie-advanced-example.html">Advanced Cookie Example</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Demonstrates using the Cookie utility to get and set subcookies.">
                                        <a href="cookie-subcookie-example.html">Subcookie Example</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
